﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; 
using DataAccessLayer; 
using System.Web.UI.WebControls;
using System.Collections.Specialized;
 
using System.Web.Mail;
using System.Net.Mail;

namespace BusinessComponents.Implementation
{
    class EmailBLL
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Booking">Booking Object of that booking</param>
        /// <param name="guest">Guest object which has email address to send</param>
        /// <param name="HostURL">The application's Host URL, http://blah.com/blah/blah</param>
        public void sendEmail(Booking booking, Guest guest, String HostURL)
        {
            StringBuilder body = new StringBuilder();
            
            
            body.Append("<html><body>");
            body.Append("Dear Customers, <br/><br/>");
            body.Append(@"Greetings from "+ Resource.HotelName+"<br/> <br/>Below is your confirmation for accommodations: <br/><br/>");
            body.Append(@"<table><tr><td>Reservation Number: </td><td>" + booking.BookingNumber + @"</td></tr>");
            body.Append(@"<tr><td>Customer Name: </td><td>" + guest.FirstName + " " + guest.LastName + @"</td></tr>");
            body.Append(@"<tr><td>Arrival Date: </td><td>" + booking.StartDate + @"</td></tr>");
            body.Append(@"<tr><td>Departure Date: </td><td>" + booking.EndDate + @"</td></tr>");
            //  body.Append(@"<tr><td>Total Guest: </td><td>" + booking.NumberOfAdults + booking.NumberOfChildren + @"</td></tr>");
            //   body.Append(@"<tr><td>Room Type: </td><td>" + booking.ReserverdRooms.First().RoomTypeName + 1 + @"</td></tr>");
            //   body.Append(@"<tr><td>Total Charges: </td><td>" + totalCost + @"</td></tr>");

           String url = HostURL+ "check/"+booking.ID;
            body.Append("</table>");
            body.Append(@"<br/><br/>");

            body.Append(@"To check your booking,<a href=" + url + ">Click here</a>.<br/>");
            body.Append(@"Note: Inclusive of 7% GST and 10% service charge.<br/>");
            body.Append(@"A deposit of S$100 will be charged upon check-in.<br/>");
            body.Append(@"The hotel's check-in time is after 12pm and check out time is before 12pm. <br/><br/><br/>");
            body.Append(@"From Marina Bay Rock Hotel");
            body.Append("</body></html>");


            //send email 
            string from = "issemailpt8@gmail.com";
            string to = guest.Email;
           
            string title = "Booking Confirmation";

            MailDefinition md = new MailDefinition();
            md.From =Resource.HotelName;
            md.IsBodyHtml = true;
            
            using (System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage())
            {
                message.From = new System.Net.Mail.MailAddress(from);
                message.To.Add(to);
                message.Subject = title;
                message.IsBodyHtml = true;
               
                message.Body = body.ToString();
                SmtpClient client = new SmtpClient("smtp.gmail.com", 587)
                {
                    Credentials = new System.Net.NetworkCredential(from, "googleisgood*"),
                    EnableSsl = true
                };
                client.Send(message);
            }           
        }

        public void sendCancelEmail(Booking booking, Guest guest, String HostURL)
        {
            StringBuilder body = new StringBuilder();


            body.Append("<html><body>");
            body.Append("Dear Customers, <br/><br/>");
            body.Append(@"Greetings from " + Resource.HotelName + "<br/> <br/>This is to inform you that your booking has cancelled upon your request: <br/><br/>");
            body.Append(@"<table><tr><td>Reservation Number: </td><td>" + booking.BookingNumber + @"</td></tr>");
            body.Append(@"<tr><td>Customer Name: </td><td>" + guest.FirstName + " " + guest.LastName + @"</td></tr>");
            body.Append(@"<tr><td>Arrival Date: </td><td>" + booking.StartDate + @"</td></tr>");
            body.Append(@"<tr><td>Departure Date: </td><td>" + booking.EndDate + @"</td></tr>");
            //  body.Append(@"<tr><td>Total Guest: </td><td>" + booking.NumberOfAdults + booking.NumberOfChildren + @"</td></tr>");
            //   body.Append(@"<tr><td>Room Type: </td><td>" + booking.ReserverdRooms.First().RoomTypeName + 1 + @"</td></tr>");
            //   body.Append(@"<tr><td>Total Charges: </td><td>" + totalCost + @"</td></tr>");

            String url = HostURL + "check/" + booking.ID;
            body.Append("</table>");
            body.Append(@"<br/><br/>");

            body.Append(@"To check your booking status,<a href=" + url + "> Click here</a>");
            body.Append(@"Non-refundable fee of 15$ will be deducted from your registered credit card.");
            body.Append(@"From Marina Bay Rock Hotel");
            body.Append("</body></html>");


            //send email 
            string from = "issemailpt8@gmail.com";
            string to = guest.Email;
            string title = "Your booking has been cancelled";

            MailDefinition md = new MailDefinition();
            md.From = Resource.HotelName;
            md.IsBodyHtml = true;

            using (System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage())
            {
                message.From = new System.Net.Mail.MailAddress(from);
                message.To.Add(to);
                message.Subject = title;
                message.IsBodyHtml = true;
                message.Body = body.ToString();
                SmtpClient client = new SmtpClient("smtp.gmail.com", 587)
                {
                    Credentials = new System.Net.NetworkCredential(from, "googleisgood*"),
                    EnableSsl = true
                };
                client.Send(message);
            }
        }
    }
}
